/*
 * Stripes1.5.java
 *
 * Created on October 23, 2008, 11:21 AM
 */
package org.netbeans.modules.web.stripes.config;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListModel;
import javax.swing.JPanel;
import javax.swing.ListModel;
import javax.swing.event.ListDataListener;

public class StripesConfigPanel extends JPanel {

    private StripesWebModuleExtender extender;

    /** Creates new form Stripes1.5VisualPanel */
    public StripesConfigPanel(StripesWebModuleExtender ext,
                              StripesConfig initialSetup) {
        initComponents();
        this.extender = ext;

        applySetup(initialSetup);
    }

    /**
     * Fills the values specified in given configurration into current panel.
     * 
     * @param initialSetup
     */
    private void applySetup(StripesConfig initialSetup) {
        if (null == initialSetup) return;
        
        // dispatcher url
        dispatcherUrlText.setText(initialSetup.getStripesDispatcherUrlPattern());

        // filter url
        filterUrlText.setText(initialSetup.getStripesFilterUrlPattern());

        // create resource bundle
        createResourcesFileComboBox.setSelected(initialSetup.isBundleCreated());

        // included localizations
        includedLocalizationsList.setModel(new LocalesListModel(
                initialSetup.getIncludedLocalizations()));
        if(initialSetup.getIncludedLocalizations().size() > 0){
            includedLocalizationsList.setSelectionInterval(
                0, initialSetup.getIncludedLocalizations().size() - 1);
        }
        
        // default localization
        defaultLocalizationComboBox.setModel(new DefaultComboBoxModel(
                initialSetup.getIncludedLocalizations().toArray()));
        defaultLocalizationComboBox.setSelectedItem(
                initialSetup.getDefaultLocalization());

        // include jstl library
        includeJSTLCheckBox.setSelected(initialSetup.isJstlIncluded());
    }

//    private List<String> getLocaleLabels(List<Locale> locales) {
//        if (null == locales) {
//            return null;
//        }
//        List<String> labels = new ArrayList<String>(locales.size());
//
//        for (Locale l : locales) {
//            labels.add(getLocaleLabel(l));
//        }
//
//        return labels;
//    }
//
//    private String getLocaleLabel(Locale locale) {
//        return "";
//    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        jTabbedPane1 = new javax.swing.JTabbedPane();
        tab1Panel = new javax.swing.JPanel();
        dispatcherUrlLabel = new javax.swing.JLabel();
        dispatcherUrlText = new javax.swing.JTextField();
        filterUrlLabel = new javax.swing.JLabel();
        filterUrlText = new javax.swing.JTextField();
        tab2Panel = new javax.swing.JPanel();
        createResourcesFileComboBox = new javax.swing.JCheckBox();
        includedLocalizationsLabel = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        includedLocalizationsList = new javax.swing.JList();
        defaultLocalizationLabel = new javax.swing.JLabel();
        defaultLocalizationComboBox = new javax.swing.JComboBox();
        tab3Panel = new javax.swing.JPanel();
        includeJSTLCheckBox = new javax.swing.JCheckBox();

        dispatcherUrlLabel.setText(org.openide.util.NbBundle.getMessage(StripesConfigPanel.class, "StripesConfigPanel.dispatcherUrlLabel.text")); // NOI18N

        dispatcherUrlText.setText(org.openide.util.NbBundle.getMessage(StripesConfigPanel.class, "StripesConfigPanel.dispatcherUrlText.text")); // NOI18N
        dispatcherUrlText.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                dispatcherUrlTextKeyReleased(evt);
            }
        });

        filterUrlLabel.setText(org.openide.util.NbBundle.getMessage(StripesConfigPanel.class, "StripesConfigPanel.filterUrlLabel.text")); // NOI18N

        filterUrlText.setText(org.openide.util.NbBundle.getMessage(StripesConfigPanel.class, "StripesConfigPanel.filterUrlText.text")); // NOI18N
        filterUrlText.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                filterUrlTextKeyReleased(evt);
            }
        });

        javax.swing.GroupLayout tab1PanelLayout = new javax.swing.GroupLayout(tab1Panel);
        tab1Panel.setLayout(tab1PanelLayout);
        tab1PanelLayout.setHorizontalGroup(
            tab1PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(tab1PanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(tab1PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(tab1PanelLayout.createSequentialGroup()
                        .addComponent(dispatcherUrlLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 220, Short.MAX_VALUE)
                        .addGap(106, 106, 106))
                    .addGroup(tab1PanelLayout.createSequentialGroup()
                        .addComponent(filterUrlLabel)
                        .addContainerGap(203, Short.MAX_VALUE))
                    .addGroup(tab1PanelLayout.createSequentialGroup()
                        .addGroup(tab1PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(filterUrlText, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(dispatcherUrlText, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 186, Short.MAX_VALUE))
                        .addContainerGap())))
        );
        tab1PanelLayout.setVerticalGroup(
            tab1PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(tab1PanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(dispatcherUrlLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(dispatcherUrlText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(filterUrlLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(filterUrlText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(40, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab(org.openide.util.NbBundle.getMessage(StripesConfigPanel.class, "StripesConfigPanel.tab1Panel.TabConstraints.tabTitle"), tab1Panel); // NOI18N

        createResourcesFileComboBox.setSelected(true);
        createResourcesFileComboBox.setText(org.openide.util.NbBundle.getMessage(StripesConfigPanel.class, "StripesConfigPanel.createResourcesFileComboBox.text")); // NOI18N
        createResourcesFileComboBox.setToolTipText(org.openide.util.NbBundle.getMessage(StripesConfigPanel.class, "StripesConfigPanel.createResourcesFile.toolTip")); // NOI18N

        includedLocalizationsLabel.setText(org.openide.util.NbBundle.getMessage(StripesConfigPanel.class, "StripesConfigPanel.includedLocalizationsLabel.text")); // NOI18N

        includedLocalizationsList.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "English (en)", "Czech (cs)" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        includedLocalizationsList.setToolTipText(org.openide.util.NbBundle.getMessage(StripesConfigPanel.class, "StripesConfigPanel.IncludedLocalizationsList.toolTip")); // NOI18N

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, createResourcesFileComboBox, org.jdesktop.beansbinding.ELProperty.create("${selected}"), includedLocalizationsList, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        jScrollPane1.setViewportView(includedLocalizationsList);

        defaultLocalizationLabel.setText(org.openide.util.NbBundle.getMessage(StripesConfigPanel.class, "StripesConfigPanel.defaultLocalizationLabel.text")); // NOI18N

        defaultLocalizationComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "English", "Czech" }));
        defaultLocalizationComboBox.setToolTipText(org.openide.util.NbBundle.getMessage(StripesConfigPanel.class, "StripesConfigPanel.DefaultLocalizationComboBox.toolTip")); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, createResourcesFileComboBox, org.jdesktop.beansbinding.ELProperty.create("${selected}"), defaultLocalizationComboBox, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        javax.swing.GroupLayout tab2PanelLayout = new javax.swing.GroupLayout(tab2Panel);
        tab2Panel.setLayout(tab2PanelLayout);
        tab2PanelLayout.setHorizontalGroup(
            tab2PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(tab2PanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(tab2PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(tab2PanelLayout.createSequentialGroup()
                        .addGroup(tab2PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(defaultLocalizationLabel)
                            .addComponent(includedLocalizationsLabel))
                        .addGap(14, 14, 14)
                        .addGroup(tab2PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1)
                            .addComponent(defaultLocalizationComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(createResourcesFileComboBox))
                .addGap(32, 32, 32))
        );
        tab2PanelLayout.setVerticalGroup(
            tab2PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(tab2PanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(createResourcesFileComboBox)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(tab2PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(defaultLocalizationLabel)
                    .addComponent(defaultLocalizationComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(tab2PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(includedLocalizationsLabel)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(20, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab(org.openide.util.NbBundle.getMessage(StripesConfigPanel.class, "StripesConfigPanel.tab2Panel.TabConstraints.tabTitle"), tab2Panel); // NOI18N

        includeJSTLCheckBox.setText(org.openide.util.NbBundle.getMessage(StripesConfigPanel.class, "StripesConfigPanel.includeJSTLCheckBox.text")); // NOI18N

        javax.swing.GroupLayout tab3PanelLayout = new javax.swing.GroupLayout(tab3Panel);
        tab3Panel.setLayout(tab3PanelLayout);
        tab3PanelLayout.setHorizontalGroup(
            tab3PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(tab3PanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(includeJSTLCheckBox)
                .addContainerGap(225, Short.MAX_VALUE))
        );
        tab3PanelLayout.setVerticalGroup(
            tab3PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(tab3PanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(includeJSTLCheckBox)
                .addContainerGap(112, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab(org.openide.util.NbBundle.getMessage(StripesConfigPanel.class, "StripesConfigPanel.tab3Panel.TabConstraints.tabTitle"), tab3Panel); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 341, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 167, Short.MAX_VALUE)
        );

        bindingGroup.bind();
    }// </editor-fold>//GEN-END:initComponents

private void dispatcherUrlTextKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_dispatcherUrlTextKeyReleased
    extender.notifyListeners(null);
}//GEN-LAST:event_dispatcherUrlTextKeyReleased

private void filterUrlTextKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_filterUrlTextKeyReleased
    extender.notifyListeners(null);
}//GEN-LAST:event_filterUrlTextKeyReleased

    public boolean getCreateResourcesFile() {
        return createResourcesFileComboBox.isSelected();
    }

    public Locale getDefaultLocalization() {
        return (Locale) defaultLocalizationComboBox.getSelectedItem();
    }

    public String getDispatcherUrl() {
        return dispatcherUrlText.getText();
    }

    public String getFilterUrl() {
        return filterUrlText.getText();
    }

    public List<Locale> getIncludedLocalizations() {
        List<Locale> locales = new ArrayList<Locale>(2);
        for (Object sl : includedLocalizationsList.getSelectedValues()) {
            locales.add((Locale)sl);
        }

        return locales;
    }

    public boolean getIncludeJSTL() {
        return includeJSTLCheckBox.isSelected();
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox createResourcesFileComboBox;
    private javax.swing.JComboBox defaultLocalizationComboBox;
    private javax.swing.JLabel defaultLocalizationLabel;
    private javax.swing.JLabel dispatcherUrlLabel;
    private javax.swing.JTextField dispatcherUrlText;
    private javax.swing.JLabel filterUrlLabel;
    private javax.swing.JTextField filterUrlText;
    private javax.swing.JCheckBox includeJSTLCheckBox;
    private javax.swing.JLabel includedLocalizationsLabel;
    private javax.swing.JList includedLocalizationsList;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JPanel tab1Panel;
    private javax.swing.JPanel tab2Panel;
    private javax.swing.JPanel tab3Panel;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables

    private static class LocalesListModel implements ListModel {

        private DefaultListModel model = new DefaultListModel();

        public LocalesListModel(List<Locale> listItems) {
            if (null == listItems) {
                return;
            }
            
            for (Locale item : listItems) {
                model.addElement(item);
            }
        }

        public int getSize() {
            return model.getSize();
        }

        public Object getElementAt(int index) {
            return model.getElementAt(index);
        }

        public void addListDataListener(ListDataListener l) {
            model.addListDataListener(l);
        }

        public void removeListDataListener(ListDataListener l) {
            model.removeListDataListener(l);
        }
    }
}